<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Class Location 地理位置
 * @author yangwu
 * 2017年6月6日16:12:35
 */

class Location extends Web_controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Qrcode_model','qrcode_model');
		$this->load->model('Location_model','location_model');
		$this->load->library('PHPExcel');
		$this->load->helper('excel');
		$this->userSession();
	}
	

    /**
     * [addLocation 添加地理位置记录]
     */
	public function addLocation()
	{
		if(trim($this->input->post('locationname'))){
			$params['location_name'] = trim($this->input->post('locationname'));//地理名
			$params['create_time'] = time();//添加时间
			if($this->location_model->addLocation($params)>0){
				header('Location:/admin/location/getlocation');
				return;
			}else{
				$this->redirect('location/addlocation','地理名已存在',1);
				return;
			}
		}else{
			$this->load->view('admin/header');
			$this->load->view('admin/addlocation');
		}
	}
	/**
	 * [getLocation 获取地理位置列表]
	 * @return [array] [列表]
	 */
	public function getLocation()
	{
		$name = $this->input->get('name');
		$data = $this->location_model->getLocations($name);
		$arr['data'] = $data;
 		$this->load->view('admin/header');
		$this->load->view('admin/addressmanagement',$arr);
	}

	/**
	 * [addScan 用户扫描添加一条记录]
	 */
	public function addScan()
	{
		$locationid = $this->input->get('locationid');
		if(!ctype_digit($locationid)){
			return;
		}
		$params['location_id'] = $locationid;
		$params['update_time'] = time();
		$params['number'] = 1;
		$this->qrcode_model->add($params);
	}
	/**
	 * [getCountByQuery 统计访问记录]
	 * @return [array] $data 
	 */
	public function getCountByQuery()
	{
		$data = $this->qrcode_model->getCountByQuery();
		//数据重构
		$arr = array();
		foreach ($data as $k => $v) {
			$v['create_time'] = date('Y-m-d H:i:s',$v['update_time']);
			unset($v['update_time']);
			$v['id'] = $k+1;
			$arr[] = $v;
		}
		//var_dump($arr);die;
		if(isset($_GET['a'])){
			$header = array('序号','地址名','访问次数','最后访问时间');
			$this->downloadLocation($header,$arr);	
		}
		$arr['data'] =$arr;
		$this->load->view('admin/header');
		$this->load->view('admin/webstatistic',$arr);
	}
	//下载网页统计
	public function downloadLocation($header,$arr)
	{
		excel_download($header,$arr);
	}
	//网站统计页面
	public function locationList()
	{
		$this->load->view('admin/header');
		$this->load->view('admin/webstatistic');
	}
}	